package com.juku2024.juku.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.juku2024.juku.R
import com.juku2024.juku.adapter.QualityAdapter
import com.juku2024.juku.model.PlayInfoResponse
import com.juku2024.juku.tools.WrapLinearLayoutManager

class SourceSelectDialog(
    context: Context, style: Int = R.style.CommonDialogStyle,
    val videoUrls: ArrayList<PlayInfoResponse>?,
    val listener: OnItemClickListener?) : Dialog(context, style) {

    private val adapter = QualityAdapter(videoUrls)

    private lateinit var tvCancel: TextView
    private lateinit var recycler: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_video_quality)

        tvCancel = findViewById(R.id.tvCancel)
        recycler = findViewById(R.id.recycler)

        setCanceledOnTouchOutside(false) //点击边际可消失
        recycler.layoutManager = WrapLinearLayoutManager(context)

        adapter.setOnItemClickListener(listener)
        recycler.adapter = adapter

        tvCancel.setOnClickListener {
            dismiss()
        }
    }

    override fun onStart() {
        super.onStart()
        window?.apply {
            this.decorView.setPadding(0, 0, 0, 0)
            val layoutParams = this.attributes
            layoutParams.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            this.attributes = layoutParams
        }
    }

    fun setNewData(videoUrls: ArrayList<PlayInfoResponse>?) {
        adapter.setList(videoUrls)
    }

}